/* Copyright 2013-2015 www.snakerflow.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.snaker.engine.entity;

import java.io.Serializable;
import java.util.Map;
import org.snaker.engine.TgSnakerUitl;
import lombok.Data;

/**
 * 历史流程实例实体类
 * 
 * @author yuqs
 * @since 1.0
 */
@Data
public class HistoryOrder implements Serializable {
    private static final long serialVersionUID = 5853727929104539328L;
    private String id; // 主键ID
    private String processId; // 流程定义ID
    private Integer orderState; // 流程实例状态（0：结束；1：活动）
    private String creator; // 流程实例创建者ID
    private String createTime; // 流程实例创建时间
    private String endTime; // 流程实例结束时间
    private String parentId; // 流程实例为子流程时，该字段标识父流程实例ID
    private String expireTime; // 流程实例期望完成时间
    private Integer priority; // 流程实例优先级
    private String orderNo; // 流程实例编号
    private String variable; // 流程实例附属变量

    public HistoryOrder() {

    }

    public HistoryOrder(Order order) {
        this.id = order.getId();
        this.processId = order.getProcessId();
        this.createTime = order.getCreateTime();
        this.expireTime = order.getExpireTime();
        this.creator = order.getCreator();
        this.parentId = order.getParentId();
        this.priority = order.getPriority();
        this.orderNo = order.getOrderNo();
        this.variable = order.getVariable();
    }

    /**
     * 根据历史实例撤回活动实例
     * 
     * @return 活动实例对象
     */
    public Order undo() {
        Order order = new Order();
        order.setId(this.id);
        order.setProcessId(this.processId);
        order.setParentId(this.parentId);
        order.setCreator(this.creator);
        order.setCreateTime(this.createTime);
        order.setLastUpdator(this.creator);
        order.setLastUpdateTime(this.endTime);
        order.setExpireTime(this.expireTime);
        order.setOrderNo(this.orderNo);
        order.setPriority(this.priority);
        order.setVariable(this.variable);
        order.setVersion(0);
        return order;
    }

    /**
     * getVariableMap
     */
    public Map<String, Object> getVariableMap() {
        return TgSnakerUitl.getVariableMap(this.variable);
    }

    /**
     * getProcessName
     */
    public String getProcessName() {
        return TgSnakerUitl.getProcessName(this.processId);
    }
}
